<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['num']?:10;
	$params=array('uniacid'=>$_SESSION['uniacid']);
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_merch_sns',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$item){
		$item['pcd']=json_decode($item['pcd'],true);
		$item['action']='jy_merch/sns//id='.$item['id'];
		$item['link']=custom_url($item['action'],true);
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['status']=array('0'=>'禁用','1'=>'启用');
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'小区管理'),
			array('op'=>'edit','title'=>'添加小区'),
			array('op'=>'link','title'=>'小区排名页面链接'),
		),
		'list'=>array('data'=>'list','del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'title','title'=>'小区名称','type'=>'string'),
			array('name'=>'image','title'=>'小区LOGO','type'=>'image'),
			array('name'=>'pcd','title'=>'所属区域','type'=>'pcd'),
			array('name'=>'address','title'=>'地址','type'=>'string'),
			array('name'=>'name','title'=>'联系人','type'=>'string'),
			array('name'=>'mobile','title'=>'联系电话','type'=>'string'),
			array('name'=>'action','title'=>'小区系统短链接','type'=>'copy'),
			array('name'=>'link','title'=>'小区长链接','type'=>'copy'),
			array('name'=>'status','title'=>'是否启用','type'=>'radio','options'=>'status'),	
				
		)),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_merch_sns',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		$item['pcd']=json_decode($item['pcd'],true);
		$item['lat_lng']=json_decode($item['lat_lng'],true);
	}else{
		$item=array();
	}
    $return['item']=$item;
	$return['status']=array('0'=>'不显示','1'=>'显示');
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'小区设置',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回小区'),
		),
		'edit'=>array(
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
			array('name'=>'title','title'=>'小区名称','type'=>'string'),
			array('name'=>'image','title'=>'小区LOGO','type'=>'image'),
			array('name'=>'pcd','title'=>'所属区域','type'=>'pcd'),
			array('name'=>'address','title'=>'地址','type'=>'string'),
			array('name'=>'lat_lng','title'=>'坐标','type'=>'lat_lng'),
			array('name'=>'name','title'=>'联系人','type'=>'string'),
			array('name'=>'mobile','title'=>'联系电话','type'=>'string'),
			array('name'=>'status','title'=>'是否启用','type'=>'switch'),		
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['title']){
		exi('请填写标题','error');
	}
	if(!$postdata['pcd']){
		exi('请选择所属区域','error');
	}
	if(!$postdata['lat_lng']){
		exi('请选择坐标','error');
	}
	$data=array_fields(array('paixu','title','pcd','image','address','status','name','mobile','lat_lng'),$postdata);
	$data['pcd']=ijson_encode($postdata['pcd']);
	$data['lat_lng']=ijson_encode($postdata['lat_lng']);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_merch_sns',$data,array('id'=>$id));
		exi('设置成功！','','/sns/');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=time();
		pdo_insert('jy_merch_sns',$data);
		exi('添加成功！','','/sns/');
	}
}
if($_RQ['op']=='link'){
	$item['snslink']='jy_merch/snslist/';
	$item['snslink2']=custom_url($item['snslink'],true);
    $return['item']=$item;
	$htmls=array(
		'type'=>'look',
		'desc'=>'小区设置',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回小区'),
		),
		'look'=>array(
			array('type'=>'looklink','title'=>'小区列表入口','name'=>'snslink'),
			array('type'=>'ewm','title'=>'小区列表入口','name'=>'snslink2'),		
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_merch_sns',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_merch_sns',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>